/**
 * 十分彩的共用逻辑
 */
(function() {
    var currTabId = 1;
    var tabDraws = [
        // 1：两面
        function() {
            fill_zonghe_row();
            fill_lmp();
        },

        // 2 单号
        fill_danhao,

        // 3 - 10 8球
        function() { fill_qiu(1); },
        function() { fill_qiu(2); },
        function() { fill_qiu(3); },
        function() { fill_qiu(4); },
        function() { fill_qiu(5); },
        function() { fill_qiu(6); },
        function() { fill_qiu(7); },
        function() { fill_qiu(8); },

        fill_zhengma,
        fill_lianma,
    ];

    // 填充总和
    function fill_zonghe_row() {
        var types = ["大", "单", "尾大", "小", "双", "尾小"];
        var table = $(".zonghe tbody");
        table.empty();
        var tr = $("<tr></tr>");
        for (var i = 0; i < types.length; i++) {
            add_bet_item('和' + types[i], '总和' + types[i], tr);
            if (i == 2) {
                table.append(tr);
                tr = $("<tr></tr>");
            }
        }
        table.append(tr);
    }

    // 填充8球的两面盘
    function fill_lmp() {
        var arr = ['一', '二', '三', '四', '五', '六', '七', '八'];
        var types = ['大', '小', '单', '双', '尾大', '尾小', '合单', '合双', '龙', '虎'];
        var arr2 = ['大', '小', '单', '双', '尾大', '尾小', '合数单', '合数双', '龙', '虎'];

        var ul = $(".q1-4");
        ul.empty();
        for (var i = 0; i < 4; i++)
        {
            var li = $('<li style="width: 24.5%"> </li>');
            ul.append(li);
            var table = $('<table class="u-table2 play_tab_64"> </table>');
            li.append(table);

            table.append('<thead><tr><th colspan="3">第__i__球</th></tr></thead>'.replace(/__i__/, arr[i]));
            var tbody = $("<tbody></tbody>");
            table.append(tbody);

            for (var j = 0; j < types.length; j++) {
                var tr = $("<tr></tr>");
                add_bet_item(arr[i] + types[j], arr2[j], tr);
                tbody.append(tr);
            }
        }

        var ul = $(".q5-8");
        ul.empty();
        for (var i = 4; i < 8; i++)
        {
            var li = $('<li style="width: 24.5%"> </li>');
            ul.append(li);
            var table = $('<table class="u-table2 play_tab_64"> </table>');
            li.append(table);

            table.append('<thead><tr><th colspan="3">第__i__球</th></tr></thead>'.replace(/__i__/, arr[i]));
            var tbody = $("<tbody></tbody>");
            table.append(tbody);

            for (var j = 0; j < types.length; j++) {
                if (j > 7) continue;
                var tr = $("<tr></tr>");
                add_bet_item(arr[i] + types[j], arr2[j], tr);
                tbody.append(tr);
            }
        }
    }
    
    // 填充8球的单号
    function fill_danhao() {
        var div = $("#tab2");
        div.empty();
        var arr = ['一', '二', '三', '四', '五', '六', '七', '八'];
        for (var z in arr) {
            var table = $('<table class="u-table2 play_tab_64 XYNCTerm mt5"> </table>');
            table.append('<thead><tr><th colspan="12">第__i__球</th></tr></thead>'.replace(/__i__/, arr[z]));
            var tbody = $("<tbody></tbody>");
            table.append(tbody);

            for (var i = 1; i <= 5; i++) {
                var tr = $("<tr></tr>");
                for (var j = i; j <= i + 15; j += 5) {
                    var k = j < 10 ? "0" + j : j;
                    var title = '<b class="b__i__">__i__</b>'.replace(/__i__/g, k);
                    if (game != 'cqkl10') {
                        title = '<span class="ball c-b1">__i__</span>'.replace(/__i__/g, k);
                    }

                    add_bet_item2(arr[z] + ":" + k, title, tr);
                }
                tbody.append(tr);
            }
            div.append(table);
        }

        var types = ['大', '单', '尾大', '合单', '小', '双', '尾小', '合双', '东', '南', '西', '北', '中', '发', '白'];
        var tables = [];
        for (var i = 1; i <= 8; i++)
            tables.push($(".q" + i + " tbody"));
        for (var z in tables) {
            var table = tables[z];
            for (var i = 1; i <= 5; i++) {
                var tr = $("<tr></tr>");
                for (var j = i; j <= i + 15; j += 5) {
                    var k = j < 10 ? "0" + j : j;
                    var title = '<b class="b__i__">__i__</b>'.replace(/__i__/g, k);
                    if (game != 'cqkl10') {
                        title = '<span class="ball c-b1">__i__</span>'.replace(/__i__/g, k);
                    }
                    add_bet_item2(arr[z] + ":" + k, title, tr);
                }
                table.append(tr);
            }

            var tr = $("<tr></tr>");
            for (var i in types) {
                var n = types[i];
                if (n == '合单') n = '合数单';
                if (n == '合双') n = '合数双';
                add_bet_item(arr[z] + types[i], n, tr);
                if (i % 4 == 3) {
                    table.append(tr);
                    tr = $("<tr></tr>");
                }
            }
            tr.append("<td colspan='3'> </td>");
            table.append(tr);

            if (z > 3) continue;
            var tr = $("<tr></tr>");
            add_bet_item(arr[z] + '龙', '龙', tr);
            add_bet_item(arr[z] + '虎', '虎', tr);
            tr.append("<td colspan='6'> </td>");
            table.append(tr);
        }
    }
    function fill_qiu(index) {
        var arr = ['一', '二', '三', '四', '五', '六', '七', '八'];
        var types = ['大', '单', '尾大', '合单', '小', '双', '尾小', '合双', '东', '南', '西', '北', '中', '发', '白'];
        var table = $(".q" + index + " tbody"); table.empty();
        var z = index - 1;
        for (var i = 1; i <= 5; i++) {
            var tr = $("<tr></tr>");
            for (var j = i; j <= i + 15; j += 5) {
                var k = j < 10 ? "0" + j : j;
                var title = '<b class="b__i__">__i__</b>'.replace(/__i__/g, k);
                if (game != 'cqkl10') {
                    title = '<span class="ball c-b1">__i__</span>'.replace(/__i__/g, k);
                }
                add_bet_item2(arr[z] + ":" + k, title, tr);
            }
            table.append(tr);
        }

        var tr = $("<tr></tr>");
        for (var i in types) {
            var n = types[i];
            if (n == '合单') n = '合数单';
            if (n == '合双') n = '合数双';
            add_bet_item(arr[z] + types[i], n, tr);
            if (i % 4 == 3) {
                table.append(tr);
                tr = $("<tr></tr>");
            }
        }
        tr.append("<td colspan='3'> </td>");
        table.append(tr);

        if (index > 4) return;
        var tr = $("<tr></tr>");
        add_bet_item(arr[z] + '龙', '龙', tr);
        add_bet_item(arr[z] + '虎', '虎', tr);
        tr.append("<td colspan='6'> </td>");
        table.append(tr);
    }

    // 填充正码
    function fill_zhengma() {
        var table = $(".zhengma tbody");
        table.empty();
        for (var i = 1; i <= 5; i++) {
            var tr = $("<tr></tr>");
            for (var j = i; j <= i + 15; j += 5) {
                var k = j < 10 ? "0" + j : j;
                var title = '<b class="b__i__">__i__</b>'.replace(/__i__/g, k);
                if (game != 'cqkl10') {
                    title = '<span class="ball c-b1">__i__</span>'.replace(/__i__/g, k);
                }
                add_bet_item2("正:" + k, title, tr);
            }
            table.append(tr);
        }

    }

    // 填充连码
    function fill_lianma() {
        var table = $("#lm_checkboxs");
        table.empty();

        // 填充20个号码
        for (var i = 1; i <= 5; i++) {
            var tr = $("<tr></tr>");
            for (var j = i; j <= i + 15; j += 5) {
                var k = j < 10 ? '0' + j : j;
                if (game == 'cqkl10')
                    tr.append('<td class="name not-event"><b class="b__i__">__i__</b></td>'.replace(/__i__/g, k));
                else
                    tr.append('<td class="name not-event"><span class="ball c-b1">__i__</span></td>'.replace(/__i__/g, k));

                tr.append('<td class="checkbox_td"><input type="checkbox" name="numbers" value="__i__"></td>'.replace(/__i__/, j));
            }
            table.append(tr);
        } 

        $(".radio_td input[type='radio']").change(function() {
            clear_lianma();
        });
        $(".checkbox_td input[type='checkbox']").change(function() {
            refresh_lianma_numbers();
        });
    }

    // 当前的投注类型
    function get_lianma_target() {
        return $(".radio_td input:checked").attr("value");
    }

    // 取得当前选中的数字列表
    function get_lianma_numbers() {
        var arr = $(".checkbox_td input:checked");
        var list = [];
        for (var i = 0; i < arr.length; i++) {
            list.push($(arr[i]).attr("value"))
        }
        return list;
    }

    // 清理选中的数字
    function clear_lianma() {
        var arr = $(".checkbox_td input:checked");
        for (var i = 0; i < arr.length; i++) $(arr[i])[0].checked = false;
        refresh_lianma_numbers();
    }

    // 刷新连码的数字信息
    function refresh_lianma_numbers() {
        var arr = $(".checkbox_td input");
        var key = get_lianma_target();
        var list = get_lianma_numbers();
        var max = 7;
        if (key == '任4' || key == '任5') max = 5;

        arr.attr("disabled", false);
        if (list.length >= max) {
            // 其他数字需要置灰
            for (var i = 0; i < arr.length; i++) {
                if (!arr[i].checked)
                    $(arr[i]).attr('disabled', true);
            }
        }

        for (var i = 0; i < arr.length; i++) {
            if (arr[i].checked) {
                $(arr[i]).parent().attr("class", "checkbox_td bg_yellow");
            }
            else {
                $(arr[i]).parent().attr("class", "checkbox_td");
            }
        }
    }

    var initF = function(game, first) {
        window.game = game;
        initGame(game, function() {
            if (false !== first)
                fillGameTabs(["两面盘", "单号1~8", "第一球", "第二球", "第三球", "第四球", "第五球", "第六球", "第七球", "第八球", "正码", "连码"]);
            currTabId = parseInt($("#cate_menus_50 .selected").attr("tid"));
            $("#tab" + currTabId).attr("flag", 1);
            var f = tabDraws[currTabId - 1];
            f();
        }, first);
    }

    // 重载确定投注的逻辑
    var getCounterListF = function(f) {
        if ($(".cate_menu a.selected").attr("tid") != 12) {
            return f();
        }

        var key = get_lianma_target();
        var limit = parseInt(key[1]);
        var list = get_lianma_numbers();
        var v = parseInt($("#bet_money1").val());
        if (v < 1 || list.length < limit) {
            layer.alert("下注内容不正确，请重新下注", {
                icon: 2
            })
            return;
        }

        key = key + ":" + list.join(",");
        var data = {};
        data[key] = v * 100;
        return data;
    }
    var resetF = function(f) {
        if ($(".cate_menu a.selected").attr("tid") != 12) {
            return f();
        }
        clear_lianma();
    }

    var parseTargetF = function(k) {
        if (k.startsWith('任2')) {
            var arr = k.split(':');
            var numbers = arr[1].split(',');
            var i = numbers.length * (numbers.length - 1) / 2;
            return '任选二 ' + arr[1] + ' [' + i + '组]';
        }
        if (k.startsWith('任3')) {
            var arr = k.split(':');
            var numbers = arr[1].split(',');
            var i = numbers.length * (numbers.length - 1) * (numbers.length - 2) / 6;
            return '任选三 ' + arr[1] + ' [' + i + '组]';
        }
        if (k.startsWith('任4')) {
            var arr = k.split(':');
            var numbers = arr[1].split(',');
            var i = numbers.length * (numbers.length - 1) * (numbers.length - 2) * (numbers.length - 3) / 24;
            return '任选四 ' + arr[1] + ' [' + i + '组]';
        }
        if (k.startsWith('组2')) {
            var arr = k.split(':');
            var numbers = arr[1].split(',');
            var i = numbers.length * (numbers.length - 1) / 2;
            return '选二连组 ' + arr[1] + ' [' + i + '组]';
        }
        if (k.startsWith('组3')) {
            var arr = k.split(':');
            var numbers = arr[1].split(',');
            var i = numbers.length * (numbers.length - 1) * (numbers.length - 2) / 6;
            return '选三前组 ' + arr[1] + ' [' + i + '组]';
        }
        return gameRule.getTargetName(k);
    }
    var calcBetF = function(data) {
        var total = 0, count = 0;
        for (var k in data) {
            if (k.startsWith('组2') || k.startsWith('任2')) {
                var arr = k.split(':');
                var numbers = arr[1].split(',');
                var i = numbers.length * (numbers.length - 1) / 2;
                count += i;
                total += data[k] * i;
                continue;
            }
            if (k.startsWith('组3') || k.startsWith('任3')) {
                var arr = k.split(':');
                var numbers = arr[1].split(',');
                var i = numbers.length * (numbers.length - 1) * (numbers.length - 2) / 6;
                count += i;
                total += data[k] * i;
                continue;
            }
            if (k.startsWith('任4')) {
                var arr = k.split(':');
                var numbers = arr[1].split(',');
                var i = numbers.length * (numbers.length - 1) * (numbers.length - 2) * (numbers.length - 3) / 24;
                count += i;
                total += data[k] * i;
                continue;
            }
            count++;
            total += data[k];
        }
        return [count, total];
    }
    var unpackF = function(data) {
        var r = {};
        for (var k in data) {
            if (k.startsWith('组2')) {
                var arr = k.split(':');
                var numbers = arr[1].split(',');
                var list = group2(numbers);
                for (var i in list) {
                    r['组2:' + list[i].join(',')] = data[k];
                }
                r.group = '选二连组 ' + arr[1] + '[' + list.length + '组]_' + get_server_time();
            }
            else if (k.startsWith('组3')) {
                var arr = k.split(':');
                var numbers = arr[1].split(',');
                var list = group3(numbers);
                for (var i in list) {
                    r['组3:' + list[i].join(',')] = data[k];
                }
                r.group = '选三前组 ' + arr[1] + '[' + list.length + '组]_' + get_server_time();
            }
            else if (k.startsWith('任2')) {
                var arr = k.split(':');
                var numbers = arr[1].split(',');
                var list = group2(numbers);
                for (var i in list) {
                    r['任2:' + list[i].join(',')] = data[k];
                }
                r.group = '任选二 ' + arr[1] + '[' + list.length + '组]_' + get_server_time();
            }
            else if (k.startsWith('任3')) {
                var arr = k.split(':');
                var numbers = arr[1].split(',');
                var list = group3(numbers);
                for (var i in list) {
                    r['任3:' + list[i].join(',')] = data[k];
                }
                r.group = '任选三 ' + arr[1] + '[' + list.length + '组]_' + get_server_time();
            }
            else if (k.startsWith('任4')) {
                var arr = k.split(':');
                var numbers = arr[1].split(',');
                var list = group4(numbers);
                for (var i in list) {
                    r['任4:' + list[i].join(',')] = data[k];
                }
                r.group = '任选四 ' + arr[1] + '[' + list.length + '组]_' + get_server_time();
            }
            else
                r[k] = data[k];
        }
        return r;
    }

    // 绘制开奖结果
    var drawF = function(list) {
        var div = $("#result_balls");
        if (game == 'cqkl10')
            div.attr("class", "T_KLSF L_XYNC");
        else
            div.attr("class", "T_KLSF L_GDKLSF");
            
        var l = '<span><b class="b__index__ ml10">__index__</b></span>';
        if (game != 'cqkl10')
            l = '<span><b class="b__index__"></b></span>';
        for (var i in list) {
            var s = l.replace(/__index__/g, list[i]);
            div.append(s);
        }
    }

    var data = {
        drawResult: drawF,
        unpack: unpackF,
        calcBetList: calcBetF,
        parseTarget: parseTargetF,
        reset: resetF,
        getCounterList: getCounterListF,
        init: initF,
    }
    context.global.game["cqkl10"] = data;
    context.global.game["gdkl10"] = data;
    context.global.game["zykl10"] = data;
})();
